package com.orm.analysis.service;

import com.orm.analysis.dao.AddressDAO;
import com.orm.analysis.dao.InterAddressDAO;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.AddressType;
import com.orm.analysis.model.InternationalAddress;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/10/13
 * Time: 11:54 AM
 * To change this template use File | Settings | File Templates.
 */

public class InterAddressService implements GenericService<InternationalAddress> {


    private static Logger log = Logger.getLogger(InterAddressService.class);

    private String interAddressDataFielDir;

    private InterAddressDAO dao;


    public InterAddressDAO getDao() {
        return dao;
    }

    public void setDao(InterAddressDAO dao) {
        this.dao = dao;
    }

    public String getInterAddressDataFielDir() {
        return interAddressDataFielDir;
    }

    public void setInterAddressDataFielDir(String interAddressDataFielDir) {
        this.interAddressDataFielDir = interAddressDataFielDir;
    }

    public InternationalAddress create(InternationalAddress entity) {
        dao.save(entity);
        return entity;
    }
    public void delete(Object id) {
        dao.delete(id);
    }
    public InternationalAddress update(InternationalAddress entity) {
        dao.update(entity);
        return entity;
    }

    public List<InternationalAddress> list(int page, int size) {
        return dao.list(page,size);
    }


    public InternationalAddress getById(Integer id) {
        return dao.getById(id);
    }

    public InternationalAddress getById(InternationalAddress InternationalAddress) {
          return dao.getById(InternationalAddress);
      }

    public void executeImproter() throws ParseException,IOException {

        try{
        log.info("File  >>" + interAddressDataFielDir);
        BufferedReader br = null;

        br = new BufferedReader(new FileReader(interAddressDataFielDir));

        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        line = br.readLine();//Skip 1 line

        List<InternationalAddress> addressList = new ArrayList<InternationalAddress>();

        while (line != null) {
            log.info("File  >>" + line);
            //ADDRESS_ID ,STREET ,ZIPCODE ,ADDRESS_TYPE ,LAST_UPDATE
            InternationalAddress address = new InternationalAddress();
            String[] data = line.split("[,]");
            address.setStreet(data[0]);
            address.setZipcode(data[1]);
            //HOME, OFFICE, SHIPPING,BILLING;
            Character character = data[2].charAt(0);

            switch (character) {
                case 'H':
                    address.setAddressType(AddressType.HOME);
                    break;
                case 'O':
                    address.setAddressType(AddressType.OFFICE);
                    break;
                case 'S':
                    address.setAddressType(AddressType.SHIPPING);
                    break;
                case 'B':
                    address.setAddressType(AddressType.BILLING);
                    break;
                default:
                    address.setAddressType(AddressType.HOME);
                    break;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(GlobalConst.DATE_PATTERN) ;

            address.setLastUpdate(simpleDateFormat.parse(data[3].trim()));






            line = br.readLine();

            addressList.add(address);

        }
           if(addressList.size()>0){

               dao.saveList(addressList);
           }


        br.close();

        }catch(Exception e ){
            e.printStackTrace();
        }

    }



}
